/**
 * Copyright 2004 - 2007 Blue Bamboo International Inc. 
 *           All rights reserved.
 *
 *
 * Blue Bamboo PROPRIETARY/CONFIDENTIAL.
 *
 */

package com.bluebamboo.bluetools.pingpang.bean;

import gnu.io.SerialPort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * DOCME
 * 
 * @author tanxuqing
 * @date Sep 13, 2009
 * @since
 */
public class ConnectionBeanManager {

	private static final ConnectionBeanManager instance = new ConnectionBeanManager();
	private ArrayList<ConnectionBean> connBeans;

	private ConnectionBeanManager() {
	}

	public static ConnectionBeanManager getInstance() {
		return instance;
	}

	/**
	 * Get the <code>ConnectionBean</code> instance with specified <code>connectionName</code>
	 * 
	 * @param connectionName
	 * @return the <code>ConnectionBean</code> instance with specified <code>connectionName</code>, or <code>null</code>
	 *         if not found
	 */
	public ConnectionBean getBean(String connectionName) {
		for (ConnectionBean bean : connBeans) {
			if (bean.getName().equals(connectionName)) {
				return bean;
			}
		}
		return null;
	}

	/**
	 * @return
	 */
	public List<ConnectionBean> listConnections() {
		if (connBeans == null) {
			connBeans = new ArrayList<ConnectionBean>();
			ConnectionBean bean = new SerialBean("COM1", "COM1", 115200, SerialPort.STOPBITS_1, SerialPort.DATABITS_8,
					SerialPort.PARITY_NONE, SerialPort.FLOWCONTROL_RTSCTS_IN);
			connBeans.add(bean);
			bean = new SocketServerBean("TCP_9900", ConnectionBean.TYPE_TCP_SERVER, 9900);
			connBeans.add(bean);
		}
		return Collections.unmodifiableList(connBeans);
	}

	/**
	 * @param newSerialBean
	 * @return <code>false</code> if the <code>newConnectionBean</code> has existed.
	 */
	public boolean addConnection(ConnectionBean newConnectionBean) {
		if (connBeans.contains(newConnectionBean))
			return false;

		connBeans.add(newConnectionBean);
		return true;
	}
}
